package dao.impl;

import dao.CountyDao;
import entity.County;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.C3P0Util;

import java.sql.SQLException;
import java.util.List;

public class CountyDaoImpl implements CountyDao {

	@Override
	public List<County> getAllCountyList(Integer cityId) {
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());

		try {
			return qr.query("select * from county where cityId = ? ", cityId,
					new BeanListHandler<County>(County.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public County getCountyByCountyId(Integer CountyId) {
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());	
		String sql="select name from county where id = ? ";
		County County=null;
		try {
			County=qr.query(sql, CountyId, new BeanHandler<County>(County.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}	
		return County;
	}
}
